package com.yljk.mydoctor.viewmodel;

import android.app.Activity;
import android.app.Application;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.yilijk.base.base.BaseViewModel;
import com.yilijk.base.network.bean.Resource;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.ClickUtils;
import com.yljk.mydoctor.R;
import com.yljk.mydoctor.entity.NewMineBean;
import com.yljk.mydoctor.model.MineModel;
import com.yljk.mydoctor.utils.MineUtils;
import com.yljk.mydoctor.utils.PopWindowUtils;
import com.yljk.servicemanager.constants.ClickApi;
import com.yljk.servicemanager.constants.ConfigApi;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.servicemanager.constants.UrlApi;
import com.yljk.servicemanager.interfaceapi.NetWorkCallBack;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2021/12/07
 */
public class MineViewModel extends BaseViewModel<MineModel> {


    public MineViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    public void createRepository() {
        super.createRepository();
        repository = new MineModel();
    }

    public MutableLiveData<Resource<NewMineBean.DataBean>> initMineUI() {
        return getRepository().getMine();
    }

    public MutableLiveData<Resource<Boolean>> getTumor() {
        return getRepository().getTumor();
    }

    public void showTumor(Activity a, boolean b) {
        if (b) {
            tomourPopupWindow.showAtLocation(a.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
            backgroundAlpha(0.3f, a);
        } else {
            PopWindowUtils.getInstance().showPopTumour1();
        }
    }

    public void push(Activity activity, String title, String pushType, String pushPage, Bundle bundle) {
        ALog.e("onMinViewClick", "title: " + title);
        if (!TextUtils.isEmpty(title)) {
            switch (title) {
                case "心心向荣": {
                    ClickUtils.sendEvent(ClickApi.Mine.Mine_xinxin, activity, "个人中心心心向荣点击");
                    break;
                }
                case "心享未来": {
                    ClickUtils.sendEvent(ClickApi.Mine.Mine_xinxiang, activity, "个人中心享未来点击");
                    break;
                }
                case "安唐保": {
                    ClickUtils.sendEvent(ClickApi.Mine.Mine_antangbao, activity, "个人中心安唐保点击");
                    break;
                }
                case "下载物料": {
                    ClickUtils.sendEvent(ClickApi.Mine.Mine_wuliao, activity, "个人中心下载物料点击");
                    break;
                }
            }
        }
        if (TextUtils.isEmpty(pushType)) return;
        if (pushType.equals("toWeb")) {
            ARouter.getInstance().build(RouterApi.WebActivity).
                    withString("title", title).
                    withString("url", pushPage).navigation();
        } else {
            if (TextUtils.isEmpty(pushType) && TextUtils.isEmpty(pushPage)) return;
            if (bundle == null) {
                MineUtils.push(activity, pushType, pushPage);
            } else {
                MineUtils.push(activity, pushType, pushPage, bundle);
            }
        }
    }

    // 设置屏幕透明度
    public void backgroundAlpha(float bgAlpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0~1.0
        activity.getWindow().setAttributes(lp);
    }


    private PopupWindow tomourPopupWindow;
    private View tomourPopupWindowView;
    private boolean tomourAgree = false;
    private RadioGroup tomourPopRadioGroup;
    private RadioButton tomourPopRadioButton1;
    private RadioButton tomourPopRadioButton2;
    private ImageView tomourPopAgree;
    private Button tomourPopSure;

    public void initPopWindow(Activity activity) {
        tomourPopupWindow = new PopupWindow();
        tomourPopupWindowView = activity.getLayoutInflater().inflate(R.layout.pop_tumour, null);
        tomourPopRadioGroup = (RadioGroup) tomourPopupWindowView.findViewById(R.id.tomour_pop_radioGroup);
        tomourPopRadioButton1 = (RadioButton) tomourPopupWindowView.findViewById(R.id.tomour_pop_radioButton1);
        tomourPopRadioButton2 = (RadioButton) tomourPopupWindowView.findViewById(R.id.tomour_pop_radioButton2);
        tomourPopAgree = (ImageView) tomourPopupWindowView.findViewById(R.id.tomour_pop_agree);
        tomourPopSure = (Button) tomourPopupWindowView.findViewById(R.id.tomour_pop_sure);
        tomourPopupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        tomourPopupWindow.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        tomourPopupWindow.setContentView(tomourPopupWindowView);
        tomourPopupWindow.setFocusable(false);
        tomourPopupWindow.setOutsideTouchable(false);
        tomourPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f, activity);
            }
        });

        ClickUtils.setFastOnClickListener(tomourPopSure, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tomourPopRadioButton1.isChecked()) {
                    tomourPopupWindow.dismiss();
                } else if (!tomourAgree && tomourPopRadioButton2.isChecked()) {
                    Toast.makeText(activity, "请同意肿瘤疾病执业承诺", Toast.LENGTH_SHORT).show();
                } else if (tomourAgree && tomourPopRadioButton2.isChecked()) {
                    tomourPopupWindow.dismiss();
                    HttpUtils.get(UrlApi.openSellTumor, new HttpCallBack<Object>() {
                        @Override
                        public void onSuccess(Object result, int code) {

                        }

                        @Override
                        public void onFailure(String failureMsg, int statusCode) {

                        }
                    });
                } else {
                    tomourPopupWindow.dismiss();
                }
            }
        });
        ClickUtils.setFastOnClickListener(tomourPopAgree, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tomourAgree) {
                    tomourAgree = false;
                    tomourPopAgree.setImageBitmap(null);
                    tomourPopAgree.setBackgroundResource(R.drawable.tomour_select_bg_no);
//                    tomourPopRadioButton1.setChecked(true);
//                    tomourPopRadioButton1.setTextColor(Color.WHITE);
//                    tomourPopRadioButton2.setTextColor(Color.BLACK);
                } else {
                    tomourAgree = true;
                    tomourPopAgree.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(), R.mipmap.select));
                    tomourPopAgree.setBackgroundResource(R.drawable.tomour_select_bg_off);
//                    tomourPopRadioButton2.setChecked(true);
//                    tomourPopRadioButton2.setTextColor(Color.WHITE);
//                    tomourPopRadioButton1.setTextColor(Color.BLACK);
                }
            }
        });
        ClickUtils.setFastOnClickListener(tomourPopRadioButton1, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                tomourAgree = false;
//                tomourPopAgree.setImageBitmap(null);
//                tomourPopAgree.setBackgroundResource(R.drawable.tomour_select_bg_no);
                tomourPopRadioButton1.setTextColor(Color.WHITE);
                tomourPopRadioButton2.setTextColor(Color.BLACK);
            }
        });
        ClickUtils.setFastOnClickListener(tomourPopRadioButton2, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                tomourAgree = true;
//                tomourPopAgree.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.select));
//                tomourPopAgree.setBackgroundResource(R.drawable.tomour_select_bg_off);
                tomourPopRadioButton2.setTextColor(Color.WHITE);
                tomourPopRadioButton1.setTextColor(Color.BLACK);
            }
        });
    }
}
